<template>
  <div class="page-manager">
    <div class="page-manager-header">
      <h3>页面管理</h3>
      <button class="create-page-btn" @click="$emit('create-page')">创建页面</button>
    </div>
    <div class="page-list">
      <div
        v-for="page in pages"
        :key="page.id"
        class="page-item"
        :class="{ active: currentPageId === page.id }"
        @click="switchToPage(page.id)"
      >
        <div class="page-item-content">
          <div class="page-item-name">
            {{ page.name }}
          </div>
          <div class="page-item-route" v-if="page.route">
            {{ page.route }}
          </div>
        </div>
        <div class="page-item-actions">
          <button class="action-btn settings-btn" @click.stop="$emit('edit-properties', page)">
            <i class="fa fa-cog"></i>
          </button>
          <button class="action-btn rename-btn" @click.stop="$emit('rename-page', page)">
            <i class="fa fa-edit"></i>
          </button>
          <button class="action-btn delete-btn" @click.stop="$emit('delete-page', page.id)">
            <i class="fa fa-trash"></i>
          </button>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { defineEmits } from 'vue';

// Props定义
const props = defineProps({
  pages: {
    type: Array,
    required: true
  },
  currentPageId: {
    type: String,
    required: true
  }
});

// Emits定义
const emit = defineEmits([
  'create-page',
  'switch-page',
  'edit-properties',
  'rename-page',
  'delete-page'
]);

// 切换页面
const switchToPage = (pageId) => {
  emit('switch-page', pageId);
};
</script>

<style scoped>
.page-manager {
  height: 100%;
  display: flex;
  flex-direction: column;
  background-color: #f8fafc;
  border-radius: 8px;
  overflow: hidden;
}

.page-manager-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 16px;
  background-color: #fff;
  border-bottom: 1px solid #e2e8f0;
}

.page-manager-header h3 {
  margin: 0;
  font-size: 16px;
  font-weight: 600;
  color: #334155;
}

.create-page-btn {
  padding: 6px 12px;
  font-size: 14px;
  background-color: #165dff;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  transition: background-color 0.2s;
}

.create-page-btn:hover {
  background-color: #0d47a1;
}

.page-list {
  flex: 1;
  overflow-y: auto;
  padding: 8px;
}

.page-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px;
  margin-bottom: 4px;
  background-color: #fff;
  border-radius: 4px;
  cursor: pointer;
  transition: all 0.2s;
  border: 1px solid transparent;
}

.page-item:hover {
  border-color: #e2e8f0;
  background-color: #f1f5f9;
}

.page-item.active {
  border-color: #165dff;
  background-color: #e0f2fe;
}

.page-item-content {
  flex: 1;
  min-width: 0;
}

.page-item-name {
  font-size: 14px;
  font-weight: 500;
  color: #334155;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.page-item-route {
  font-size: 12px;
  color: #64748b;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  margin-top: 2px;
}

.page-item-actions {
  display: flex;
  gap: 4px;
  opacity: 0;
  transition: opacity 0.2s;
}

.page-item:hover .page-item-actions {
  opacity: 1;
}

.action-btn {
  width: 24px;
  height: 24px;
  display: flex;
  align-items: center;
  justify-content: center;
  border: none;
  border-radius: 4px;
  background-color: transparent;
  cursor: pointer;
  transition: background-color 0.2s;
  color: #64748b;
}

.action-btn:hover {
  background-color: #e2e8f0;
}

.settings-btn:hover {
  color: #165dff;
}

.rename-btn:hover {
  color: #22c55e;
}

.delete-btn:hover {
  color: #ef4444;
}
</style>